#!/bin/sh

# must not fail
#set -e 

# debug
#set -x 

echo "Starting $0"

PATHENV=/etc/certificate-services.conf


echo "Welcome to $0, a tool to install ejbca-extra-ca-service"
SetupLogFile="/tmp/ejbca-extra-ca-service-setup.$$"

runningid=`id -u`
if [ "x${runningid}" != "x0" ] ; then
    echo "must run as root eg: sudo $0"
    exit
fi

if [ -r ${PATHENV} ] ; then
    source ${PATHENV}
else
    echo "${PATHENV} missing"
    exit
fi


echo "configuring External RA"

# fix ds settings in extra/src/appserver/jboss/ramessage1-ds.xml
echo "Enter ipaddress for external ra1"
read extraip1
perl -pi -e "s#jdbc:mysql://127.0.0.1/messages#jdbc:mysql://${extraip1}/messages#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Enter username in mysql for external ra1"
read extrauser1
perl -pi -e "s#user-name>ejbca</user-name#user-name>${extrauser1}</user-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Enter password in mysql for external ra1"
read extrapass1
perl -pi -e "s#password>ejbca</password#password>${extrapass1}</password#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml

echo "Configure a secondary external RA (y/n)?"
read ra2
if [ "x${ra2}" = "xy" -o "x${ra2}" = "xY" ] ; then
	cp ${EXTRA_HOME}/src/appserver/jboss/ramessage1-ds.xml ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml	
	perl -pi -e "s#jndi-name>RAMessage1DS</jndi-name#jndi-name>RAMessage2DS</jndi-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml
	chown -R  ${appsrvuser}.${appsrvgrp} ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml
	chmod 600                            ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	cp ${EXTRA_HOME}/src/db/hibernate1.cfg.xml ${EXTRA_HOME}/src/db/hibernate2.cfg.xml
	perl -pi -e "s#hibernate.connection.datasource\">java:/RAMessage1DS</property#hibernate.connection.datasource\">java:/RAMessage2DS</property#gio" ${EXTRA_HOME}/src/db/hibernate2.cfg.xml
	chown -R  ${appsrvuser}.${appsrvgrp} ${EXTRA_HOME}/src/db/hibernate2.cfg.xml
	chmod 600                            ${EXTRA_HOME}/src/db/hibernate2.cfg.xml


	echo "Enter ipaddress for external ra2"
	read extraip2
	perl -pi -e "s#jdbc:mysql://.*/messages#jdbc:mysql://${extraip2}/messages#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	echo "Enter username in mysql for external ra2"
	read extrauser2
	perl -pi -e "s#user-name>ejbca</user-name#user-name>${extrauser2}</user-name#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml

	echo "Enter password in mysql for external ra2"
	read extrapass2
	perl -pi -e "s#password>ejbca</password#password>${extrapass2}</password#gio" ${EXTRA_HOME}/src/appserver/jboss/ramessage2-ds.xml


fi


# fix extra/conf/externalra-caservice.properties

perl -pi -e "s/#externalra-caservice.hibernateresource=hibernate1.cfg.xml/externalra-caservice.hibernateresource=hibernate1.cfg.xml/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

echo "Require encryption (y/n)?"
read extraenc
if [ "x${extraenc}" = "xy" -o "x${extraenc}" = "xY" ] ; then
	perl -pi -e "s/#externalra-caservice.encryption.required=false/externalra-caservice.encryption.required=true/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties
fi

echo "Sign messages (y/n)?"
read extrasign
if [ "x${extrasign}" = "xy" -o "x${extrasign}" = "xY" ] ; then
	perl -pi -e "s/#externalra-caservice.signature.required=false/externalra-caservice.signature.required=true/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

fi

echo "Enter the CA issuing the RA Certificates"
read extraca
perl -pi -e "s/#externalra-caservice.raissuer=AdminCA1/externalra-caservice.raissuer=${extraca}/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

echo "Enter the absolute filename of extrakeystore.p12"
read extrakeystore
mkdir ${EXTRA_HOME}/keystore
if [ -f ${extrakeystore} ] ; then
	cp ${extrakeystore} ${EXTRA_HOME}/keystore/extrakeystore.p12
else
	echo "couldn't find that file ${extrakeystore}, config/copy to ${EXTRA_HOME}/keystore/extrakeystore.p12 manually"
fi
	
echo "Enter password for extrakeystore.p12"
read extrakeystorepass
perl -pi -e "s/#externalra-caservice.keystore.pwd=foo123/externalra-caservice.keystore.pwd=${extrakeystorepass}/gio" ${EXTRA_HOME}/conf/externalra-caservice.properties

# set search path to extra in ejbca
if [ ! -f ${EJBCA_HOME}/conf/externalra.properties ] ; then
	cp ${EJBCA_HOME}/conf/externalra.properties.sample ${EJBCA_HOME}/conf/externalra.properties
	chown -R ${JBOSSUS}.${JBOSSGRP} ${EJBCA_HOME}/conf/externalra.properties
	chmod 600 ${EJBCA_HOME}/conf/externalra.properties
fi
perl -pi -e "s#.*externalra.home=.*#externalra.home=${EXTRA_HOME}#gio" ${EJBCA_HOME}/conf/externalra.properties

# deploy keystore 
cd ${EXTRA_HOME}

chown -R ${JBOSSUS}.${JBOSSGRP} ${INSTALL_INTO}/${EXTRA_DIRNAME}/
    
su -p -s /bin/sh ${JBOSSUS} -c "ant deploy-keystore" || echo "build filed"
if [ $? -ne 0 ] ; then
        echo "FAILED: ant deploy-keystore failed"
        echo "config/copy to ${EXTRA_HOME}/keystore/extrakeystore.p12 manually"
fi

echo "Step 1. run <smartcard20>/ejbca-build-and-deploy"
echo "Step 2. Go to ejbca adminweb, Edit Services and add a new service called ExternalRA1, or similar. "
echo "Step 3. Edit the service:"
echo "Select Worker: Custom worker"
echo "Custom Worker Class Path: org.ejbca.extra.caservice.ExtRACAServiceWorker"
echo "Custom Worker Properties: (optionally you can configure all properties here. See conf/externalra-caservice.properties for information.)"
echo "Custom Worker Properties: externalra-caservice.hibernateresource=hibernate1.cfg.xml"

if [ "x${ra2}" = "xy" -o "x${ra2}" = "xY" ] ; then
	echo "also configure a secondary external RA called ExternalRA2"
	echo "in Custom Worker Properties: externalra-caservice.hibernateresource=hibernate2.cfg.xml"
fi
echo "End of $0"


